<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      class="form-search"
      label-width="150px">
      <!-- <el-form-item :label="$t('mtd.exchangeName')"
        prop="exchangeName">
        <el-input v-model="queryParams.exchangeName"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="100"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item :label="$t('mtd.exchangeName')"
        prop="exchangeId">
        <el-select v-model="queryParams.exchangeId"
          filterable
          remote
          reserve-keyword
          :placeholder="$t('common.placeholderSelect')"
          :remote-method="remoteMethod"
          :loading="exhloading"
          style="width: 240px">
          <el-option v-for="item in exchangeList"
            :key="item.exchangeId"
            :label="item.exchangeName"
            :value="item.exchangeId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('mtd.transCode')"
        prop="transCode">
        <el-input v-model="queryParams.transCode"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('mtd.ToAccountNumber')"
        prop="toAccountNo">
        <el-input v-model="queryParams.toAccountNo"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('mtd.transDate')">
        <el-date-picker v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('common.startDate')"
          :end-placeholder="$t('common.endDate')"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('mtd.status')"
        prop="status">
        <el-select v-model="queryParams.status"
          :placeholder="$t('common.placeholderSelect')"
          clearable
          style="width: 240px">
          <el-option v-for="dict in dict.type.transfer_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <div class="section-box">
      <el-table v-loading="loading"
        :data="detailList">
        <!-- <el-table-column type="selection"
        min-width="55"
        align="center" /> -->
        <el-table-column :label="$t('mtd.exchangeName')"
          prop="exchangeName"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('mtd.amount')"
          prop="amount"
          min-width="120" />
        <el-table-column :label="$t('mtd.rpaAmount')"
          prop="rpaAmount"
          min-width="120" />
        <el-table-column :label="$t('mtd.rate')"
          prop="rate"
          min-width="80" />
        <el-table-column :label="$t('mtd.uAmount')"
          prop="uAmount"
          min-width="120" />
        <el-table-column :label="$t('mtd.toBankName')"
          prop="toBankName"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column :label="$t('mtd.ToAccountNumber')"
          prop="toAccountNo"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column :label="$t('mtd.transCode')"
          prop="transCode"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column :label="$t('mtd.transDate')"
          prop="transDate"
          min-width="160" />
        <el-table-column :label="$t('mtd.status')"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.transfer_status"
              :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.createBy')"
          align="center"
          prop="createBy"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('common.createTime')"
          align="center"
          prop="createTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.updateBy')"
          align="center"
          prop="updateBy"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('common.updateTime')"
          align="center"
          prop="updateTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.controls')"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="160">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)">{{$t('common.view')}}</el-button>
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.status !== '1'"
              @click="handleStatusChange(scope.row)">{{$t('mtd.changeStaus')}}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 查看详情 -->
    <ViewShow :title="$t('common.viewDetail')"
      :open="showView"
      :dataList="viewList"
      :isLine="true"
      :labelWidth="160"
      width="800"
      @toClose="showView = false" />

    <!-- 修改状态 -->
    <el-dialog :title="$t('mtd.changeTransferStatus')"
      :visible.sync="open"
      width="500px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="80px">
        <el-form-item :label="$t('mtd.status')"
          prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.transfer_status"
              :key="dict.value"
              :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('common.remark')"
          prop="remark"
          v-if="form.status == '2'">
          <el-input v-model="form.remark"
            type="textarea"
            maxlength="200"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>

        <template v-if="form.status == '1'">
          <el-form-item :label="$t('mtd.rate')"
            prop="rate">
            <div style="display: flex; align-items: center">
              <span style="color: #606266;font-size: 14px"
                class="mr10">1U =</span>
              <el-input v-model="form.rate"
                style="flex: 1;"
                @input="form.rate = onInput(form.rate)"
                :placeholder="$t('common.placeholderContent')">
                <template slot="append">{{$t('common.unit')}}</template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item :label="$t('mtd.amount')"
            prop="amount">
            <el-input v-model="form.amount"
              maxlength="20"
              @input="form.amount = onInput(form.amount)"
              :placeholder="$t('common.placeholderContent')">
              <template slot="append">{{$t('common.U')}}</template>
            </el-input>
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">{{$t('common.ok')}}</el-button>
        <el-button @click="cancel">{{$t('common.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  merchantDetailList,
  merchantTransferDetail,
  changeMerchantTransferStatus,
} from "@/api/transaction/merchantDetail.js";
import ViewShow from '@/views/components/viewShow.vue'
import {
  getExhUserList,
} from "@/api/system/exchangeUser.js";
import { onInput } from '@/utils/util.js'

export default {
  name: "merchantDetail",
  dicts: ["transfer_status"],
  components: { ViewShow },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        exchangeName: null,
        exchangeId: null,
        toAccountNo: null,
        transCode: null,
        startTime: null,
        endTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      rules: {
        remark: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
        rate: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
        amount: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
      },

      showView: false,
      viewList: [],
      exhloading: false,
      exchangeList: []
    };
  },
  watch: {
    dateRange (val) {
      if (val) {
        this.queryParams.startTime = `${val[0]} 00:00:00`
        this.queryParams.endTime = `${val[1]} 23:59:59`
      } else {
        this.queryParams.startTime = null
        this.queryParams.endTime = null
      }
    },
  },
  created () {
    this.getList();
  },
  methods: {
    onInput,
    // 搜索汇兑用户
    remoteMethod (query) {
      if (query !== '') {
        this.exhloading = true;
        getExhUserList({
          pageNum: 1,
          pageSize: 10,
          exchangeName: query,
          status: 1,
          payType: 'payin'
        }).then(
          (response) => {
            this.exchangeList = response.rows;
            this.exhloading = false;
          }
        );
      } else {
        this.exchangeList = [];
      }
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      if (!this.dateRange.length) {
        this.queryParams.startTime = null
        this.queryParams.endTime = null
      }
      merchantDetailList(this.queryParams).then(
        (response) => {
          this.detailList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // 查看详情
    handleView (row) {
      this.viewList = []
      merchantTransferDetail(row.transId).then((response) => {
        let statusList = this.dict.type.transfer_status
        let viewInfo = response.data;
        let showData = ['exchangeName', 'amount', 'rate', 'transCode', 'transDate', 'status', 'fromBankName', 'toBankName',
          'fromAccountName', 'toAccountName', 'fromIfscCode', 'toIfscCode']
        let commonData = ['remark', 'createBy', 'createTime', 'updateBy', 'updateTime']

        showData.forEach(item => {
          this.viewList.push({
            type: 'text',
            label: this.$t(`mtd.${item}`) + '：',
            value: item == 'status' ? statusList.filter(i => viewInfo[item] == i.value)[0].label : viewInfo[item]
          })
        })
        commonData.forEach(item => {
          this.viewList.push({
            type: 'text',
            label: this.$t(`common.${item}`) + '：',
            value: viewInfo[item]
          })
        })
        if (viewInfo.attachment) {
          let imgs = viewInfo.attachment.split(',')
          this.viewList.push({
            type: 'img',
            label: this.$t(`mtd.attachment`) + '：',
            value: imgs
          })
        } else {
          this.viewList.push({
            type: 'img',
            label: this.$t(`mtd.attachment`) + '：',
            value: []
          })
        }

        setTimeout(() => {
          this.showView = true;
        })
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.dateRange = []
      this.queryParams.startTime = null
      this.queryParams.endTime = null
      this.handleQuery();
    },
    // 表单重置
    reset () {
      this.form = {
        transId: null,
        status: null,
        remark: null,
        amount: null,
        rate: null
      }
      this.resetForm("form");
    },
    // 状态修改
    handleStatusChange (row) {
      this.reset()
      this.form.transId = row.transId
      this.form.status = row.status
      this.form.remark = row.remark
      this.form.amount = row.amount
      this.form.rate = row.rate
      this.open = true;
    },
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          changeMerchantTransferStatus(this.form).then((response) => {
            this.$modal.msgSuccess(this.$t('common.changeSuccess'));
            this.open = false;
            this.getList();
          });
        }
      });
    },
    cancel () {
      this.open = false;
      this.reset()
    },
  },
};
</script>
